import { http } from "@/utils/http";

// 菜单管理相关接口
// 一级菜单列表
export const getMenu1List = () => {
  return http.request<any>("get", "/api/v1/system/menu/menu1_list");
};

// 创建一级菜单
export const createMenu1 = (data: { menu1Name: string; menu1Key: string }) => {
  return http.request<any>("post", "/api/v1/system/menu/menu1_create", {
    data
  });
};

// 二级菜单列表
export const getMenu2List = (menu1Id: number) => {
  return http.request<any>("get", "/api/v1/system/menu/menu2_list", {
    params: { menu1Id }
  });
};

// 创建二级菜单
export const createMenu2 = (data: {
  menu2Name: string;
  menu2Key: string;
  menu1Id: number;
}) => {
  return http.request<any>("post", "/api/v1/system/menu/menu2_create", {
    data
  });
};

// 事件列表
export const getActionList = (menu2Id?: number) => {
  return http.request<any>("get", "/api/v1/system/menu/action_list", {
    params: { menu2Id }
  });
};

// 创建事件
export const createAction = (data: {
  menu2Name: string;
  menu2Key: string;
  menu1Id: number;
}) => {
  return http.request<any>("post", "/api/v1/system/menu/action_create", {
    data
  });
};

// 角色管理相关接口
// 获取角色列表
export const getRoles = (departId?: number) => {
  return http.request<any>("get", "/api/v1/system/role/list", {
    params: { departId }
  });
};

// 创建角色
export const createRole = (data: {
  roleName: string;
  actionIdList: number[];
  departId: number;
}) => {
  return http.request<any>("post", "/api/v1/system/role/create", { data });
};

// 更新角色
export const updateRole = (data: {
  roleId: number;
  roleName: string;
  actionIdList: number[];
}) => {
  return http.request<any>("post", "/api/v1/system/role/update", { data });
};

// 部门管理相关接口
// 获取部门列表
export const getDepartList = () => {
  return http.request<any>("get", "/api/v1/system/depart/list");
};

// 获取用户列表
export const getAccountList = params => {
  return http.request<any>("get", "/api/v1/account/list", { params });
};

// 添加用户
export const addUser = (data: any) => {
  return http.request<any>("post", "/api/v1/system/user/add", { data });
};

// 编辑用户
export const editUser = (data: any) => {
  return http.request<any>("put", "/api/v1/system/user/edit", { data });
};

// 删除用户
export const deleteUser = (userId: string) => {
  return http.request<any>("post", "/api/v1/account/delete", {
    data: {
      accountId: Number(userId)
    }
  });
};

// 编辑用户账号密码
export const editUserPassword = (data: any) => {
  return http.request<any>("post", "/api/v1/account/password/edit", {
    data: {
      accountId: Number(data.accountId),
      password: data.password
    }
  });
};

// 检查邮箱是否存在
export const checkEmailExist = (email: string) => {
  return http.request<any>("get", `/api/v1/system/user/check-email/${email}`);
};

// 分配广告账户
export const assignAccounts = (data: any) => {
  return http.request<any>("post", "/api/v1/system/user/assign-accounts", {
    data
  });
};

// 获取广告账户明细
export const getAccountDetails = (userId: string) => {
  return http.request<any>(
    "get",
    `/api/v1/system/user/account-details/${userId}`
  );
};

// 获取权限树
export const getPermissionTree = () => {
  return http.request<any>("get", "/api/v1/system/permission/tree");
};

// 保存权限配置
export const savePermissions = (data: any) => {
  return http.request<any>("post", "/api/v1/system/permission/save", { data });
};

// 绑定角色
export const bindRole = (data: { accountId: number; roleId: number }) => {
  return http.request<any>("post", "/api/v1/account/bind/role", { data });
};

// 获取广告账户列表
export const getAdAccountList = (
  page: number,
  size: number,
  adAccountName?: string
) => {
  const params: any = { page, size };
  if (adAccountName) {
    params.adAccountName = adAccountName;
  }
  return http.request<any>("get", "/api/v1/system/all/adAccount", {
    params
  });
};

// 绑定广告账户
export const bindAdAccount = (data: {
  accountId: number;
  adAccountList: {
    adAccountId: number;
    adAccountName: string;
  }[];
}) => {
  return http.request<any>("post", "/api/v1/account/bind/adAccount", { data });
};

// 角色权限树接口返回类型
interface RolePermissionResponse {
  code: number;
  msg: string;
  data: {
    menu1List: Array<{
      menu1Id: number;
      menu1Name: string;
      menu1Key: string;
      menu2List: Array<{
        menu2Id: number;
        menu2Name: string;
        menu2Key: string;
        menu1Id: number;
        actionList: Array<{
          menu2Id: number;
          actionId: number;
          actionName: string;
          actionKey: string;
        }>;
      }>;
    }>;
  };
}

// 获取角色权限树
export const getRolePermission = (roleId: number) => {
  return http.request<RolePermissionResponse["data"]>(
    "get",
    "/api/v1/system/role/permission",
    {
      params: { roleId }
    }
  );
};
